package cerebrum.controller.wallpaper;

import cerebrum.common.FilePathUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 首次运行将默认的6张壁纸写入本地，实现壁纸替换，避免给人一直软件没有有运行的错觉
 */
public class FirstRunImagesWrite {
    /**
     * 将默认的壁纸图片写入本地
     */
    public void firstRunImagesWrite() {
        String imagePath = FilePathUtil.getDefaultImagesPath();

        for (int i = 1; i < 7; i++) {
            String imageName = i + ".jpg";

            File file = new File(imagePath + File.separator + imageName);
            if (!file.exists()) {
                file.getParentFile().mkdirs();
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try (FileOutputStream out = new FileOutputStream(file);
                 InputStream is = this.getClass().getResourceAsStream("/fxml/css/images/wallpaper/" + imageName);
            ) {
                int len = -1;
                byte[] buffer = new byte[1024];
                while ((len = is.read(buffer)) != -1) {
                    out.write(buffer, 0, len);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
